/* vim:ft=objc:
 * $Id: Account.m 139 2005-05-21 17:35:44Z bmoore $
 *
 * Copyright (c) 2004, 2005  Branden J. Moore.
 *
 * This file is part of MacBiff, and is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * MacBiff is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MacBiff; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307 USA.
 *
 */

#import <Cocoa/Cocoa.h>

#import "Account.h"
#import "Folder.h"
#import "PreferencesController.h"
#import "debug.h"

@implementation Account

+ (NSString*) plugInName
{
	[self doesNotRecognizeSelector: _cmd];
	return nil;
}

+ (NSString*) plugInType
{
	return ACCOUNT_PLUGIN_TYPE;
}


- (id) initWithName: (NSString*) n
{
	if ( ( self = [super init] ) != Nil ) {
		[self setName: n];
		folders = [[NSMutableArray alloc] initWithCapacity: 5];
	}
	return self;
}


- (void) dealloc
{
	if ( acctName ) [acctName release];
	[super dealloc];
}


- (void) setActivityViewController: (ActivityViewController*) avc
{
	actControl = avc;
}


- (void) setName: (NSString*) n
{
	NSString *on = acctName;
	acctName = [n copy];

	if ( on ) {
		/* do the rename stuff in prefs */
		[prefs renameAccount: on to: acctName];
		[on release];
	}
}


- (NSString*) name
{
	return acctName;
}


- (BOOL) enabled
{
	return [[[prefs account: acctName] objectForKey: @"Enabled"] boolValue];
}


- (void) setEnabled: (BOOL) e
{
	[[prefs account: acctName] setObject: [NSNumber numberWithBool: e]
				forKey: @"Enabled"];
	[prefs commitAccounts];
}


- (void) setupView
{
	[self doesNotRecognizeSelector: _cmd];
}


- (int) checkMail
{
	[self doesNotRecognizeSelector: _cmd];
	return 0;
}



- (NSArray*) arrayOfMenuItemsFrom: (NSArray*) folArr
	     withDepth: (int) depth
{
	NSMutableArray *arr = [[NSMutableArray alloc] init];
	Folder *folder = Nil;
	NSEnumerator *folEnum = [folArr objectEnumerator];

	while ( (folder = [folEnum nextObject]) ) {
		if ( ![folder wantsShown] ) continue;

		NSString *tstr = Nil;
		if ( [folder canContainMail] ) {
			tstr = [NSString stringWithFormat: @"%@ [%u/%u]",
				[folder name],
				[folder unreadMail],
				[folder totalMail]];
		} else {
			tstr = [NSString stringWithString: [folder name]];
		}

		NSMenuItem *mi = [[NSMenuItem alloc] initWithTitle: tstr
				action: Nil keyEquivalent: @""];
		[mi setRepresentedObject: folder];
		[mi setIndentationLevel: depth];

		if ( [folder unreadMail] ) {
			[mi setTarget: folder];
			[mi setAction: @selector(touched:)];

			[mi setState: [folder masked] ? NSOffState : NSOnState];
			[mi setSubmenu: [folder messageMenu]];
		}

		[arr addObject: [mi autorelease]];

		[arr addObjectsFromArray: [self arrayOfMenuItemsFrom:
						[folder subFolders]
					withDepth: (depth+1)]];
	}

	return [arr autorelease];
}


- (void) stopNow: (id) obj
{
	[self doesNotRecognizeSelector: _cmd];
}


- (NSArray*) arrayOfMenuItems
{
	return [self arrayOfMenuItemsFrom: folders withDepth: 1];
}


- (unsigned int) total
{
	[self doesNotRecognizeSelector: _cmd];
	return 0;
}


- (unsigned int) unread
{
	[self doesNotRecognizeSelector: _cmd];
	return 0;
}


- (unsigned int) totalUnread
{
	unsigned int tot = 0;
	NSArray *fols = [self foldersWithUnreadMailIncludingMasked: YES];
	NSEnumerator *folEnum = [fols objectEnumerator];
	Folder *fol = Nil;

	while ( (fol = [folEnum nextObject]) ) {
		tot += [fol unreadMail];
	}

	return tot;
}


- (unsigned int) new
{
	[self doesNotRecognizeSelector: _cmd];
	return 0;
}


- (unsigned int) totalNew
{
	unsigned int tot = 0;
	NSArray *fols = [self foldersWithNewMailIncludingMasked: YES];
	NSEnumerator *folEnum = [fols objectEnumerator];
	Folder *fol = Nil;

	while ( (fol = [folEnum nextObject]) ) {
		tot += [fol unreadMail];
	}

	return tot;

}


/* Returns All Folders, hierarchically */
- (NSArray*) allFolders
{
	return folders;
}


- (NSArray*) folderSelect: (NSArray*) folArr
	       withSelector: (SEL) sel
	       withMasked: (BOOL) mask
{
	Folder *folder = Nil;
	NSEnumerator *folEnum = [folArr objectEnumerator];
	NSMutableArray *list = [[NSMutableArray alloc] initWithCapacity: 5];
	BOOL res;
	NSMethodSignature *aSig = Nil;
	NSInvocation *invoker = Nil;

	aSig = [Folder instanceMethodSignatureForSelector: sel];
	invoker = [NSInvocation invocationWithMethodSignature: aSig];
	[invoker setSelector: sel];

	while ( (folder = [folEnum nextObject]) ) {
		[invoker invokeWithTarget: folder];
		[invoker getReturnValue: &res];
		if ( res ) {
			if ( mask || ![folder masked] ) {
				[list addObject: folder];
			}
		}
		[list addObjectsFromArray: [self folderSelect:
							[folder subFolders]
						withSelector: sel
						withMasked: mask]];
	}

	return [list autorelease];
}


/* Returns a list of folders.  No Hierarchy */
- (NSArray*) foldersWithNewMailIncludingMasked: (BOOL) use_mask
{
	return [self folderSelect: folders
			withSelector: @selector(hasNew:)
			withMasked: use_mask];
}


/* Returns a list of folders.  No Hierarchy */
- (NSArray*) foldersWithUnreadMailIncludingMasked: (BOOL) use_mask
{
	return [self folderSelect: folders
			withSelector: @selector(hasUnread:)
			withMasked: use_mask];
}


- (unsigned int) mailInFolder: (NSString*) folder
{
	[self doesNotRecognizeSelector: _cmd];
	return 0;
}


- (unsigned int) unreadMailInFolder: (NSString*) folder
{
	[self doesNotRecognizeSelector: _cmd];
	return 0;
}


- (unsigned int) newMailInFolder: (NSString*) folder
{
	[self doesNotRecognizeSelector: _cmd];
	return 0;
}



@end
